package demo.service;

import demo.domain.DemoAddress;
import demo.domain.DemoUser;
import demo.domain.dao.DemoAddressDao;
import demo.domain.dao.DemoUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by lliu on 2015/11/6.
 */
@Service
public class DemoUserServiceImpl implements DemoUserService {

    @Autowired
    private DemoUserDao demoUserDao;

    @Autowired
    private DemoAddressDao demoAddressDao;

    @Autowired
    private DemoAddressService demoAddressService;

    @Transactional
    @Override
    public void save(DemoUser demoUser) {
        this.demoUserDao.save(demoUser);
    }

    @Transactional
    @Override
    public void delete(DemoUser demoUser) {
        this.demoUserDao.delete(demoUser);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        this.demoUserDao.delete(id);
    }

    @Transactional
    @Override
    public void update(DemoUser demoUser) {
        this.demoUserDao.update(demoUser);
    }

    @Override
    public List<DemoUser> queryAllUsers() {
        return this.demoUserDao.loadAll();
    }

    @Override
    public DemoUser get(Long id) {
        return this.demoUserDao.get(id);
    }

    @Transactional
    @Override
    public void addDemoUserAndAddress(DemoUser demoUser, DemoAddress demoAddress) {
        this.demoUserDao.save(demoUser);
        this.demoAddressDao.save(demoAddress);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void editDemoUserAndAddress(DemoUser demoUser, DemoAddress demoAddress) {
        this.demoUserDao.update(demoUser);
        this.demoAddressService.editDemoAddress(demoAddress);
    }
}
